var DBFactory = require('../db/mysql_db');
var appkeyDao = module.exports;
require('../util/common.js');

appkeyDao.createAppkey = function(data, cb) {
    if(!data) return cb(new ERR(500));
    var arr = {};
    arr.uid  = data.uid;
    arr.app_sid = data.app_sid;
    arr.created_time = new Date();
    arr.appkey = data.uid + '' + randomString(5);
    var sql = 'replace into app_appkey set  ? ';
    DBFactory.poolQuery(sql, arr,
        function () {
            cb('no collection');
        },
        function (err, result) {
            if (err) {
                console.log(err);
                cb(err);
                return;
            }
            if(result && result.affectedRows > 0) {
                cb(null, arr.appkey);
            } else {
                cb('申请appkey发生错误，请重试！');
            }
        }
    );
}